package com.example.administrator.android2019.ch2Activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.android2019.R;

 /**
  *  使用的布局文件：listview 控件
  *  列表中每一项显示的内容，使用另一个布局文件：IMageView+TextView
  * */

public class MarketActivity extends AppCompatActivity {
  private ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_market);
        listView=findViewById(R.id.market_listview);
       final MyAapter adapda=new MyAapter();
        listView.setAdapter(adapda);
        /*点击每一项，Toast内容*/
        AdapterView.OnItemClickListener listener=new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String s=adapda.names[position];
                Toast.makeText(MarketActivity.this,s,Toast.LENGTH_SHORT).show();
            }
        };
        listView.setOnItemClickListener(listener);
    }
    class MyAapter extends BaseAdapter{
        /*定义列表项要显示的图片和文字数组*/
        int[] img={R.drawable.jd,R.drawable.qq,R.drawable. dz,R.drawable.xl,R.drawable.tm,
                R.drawable.uc,R.drawable.wx};
        String[] names={"京东商城","QQ","QQ斗地主" ,"新浪微博","天猫商城","UC浏览器","微信"};


        @Override
        public int getCount() {
            return names.length;
        }

        @Override
        public Object getItem(int position) {
            return names[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            /**
             * 把内容（图片+文字 数组） 展示在控件（ImageView+TextView），
             * 1、关联布局文件
             * 2、关联
             *
             * */
           /** View view=View.inflate(MarketActivity.this,R.layout.activity_marketitem,null);
            ImageView imageView=view.findViewById(R.id.image_list);
            TextView textView=view.findViewById(R.id.tv_list);

            imageView.setImageResource(img[position]);
            textView.setText(names[position]);
            return view;*/
           ViewHolder viewHolder=new ViewHolder();
           if (convertView==null){
               convertView=View.inflate(MarketActivity.this,R.layout.activity_marketitem,null);
               viewHolder.imageView=convertView.findViewById(R.id.image_list);
               viewHolder.textView=convertView.findViewById(R.id.tv_list);
               convertView.setTag(viewHolder);
           }else {
               viewHolder= (ViewHolder) convertView.getTag();
           }
           viewHolder.imageView.setImageResource(img[position]);
           viewHolder.textView.setText(names[position]);
           return convertView;
        }
    }
    /*将要加载的子view（每一项item）放在viewHolder类*/
    class  ViewHolder{
        ImageView imageView;
        TextView textView;
    }
}
